{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": 3
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python_defaultSpec_1595309653545",
   "display_name": "Python 3.7.4 64-bit ('tensorflow': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据统计\n",
    "import tensorflow as tf\n",
    "a = tf.ones([2,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<tf.Tensor: shape=(), dtype=float32, numpy=2.0>"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "tf.norm(a)  # 求范数默认2范数,所有元素平方和开根号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<tf.Tensor: shape=(), dtype=float32, numpy=2.0>"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "tf.sqrt(tf.reduce_sum(tf.square(a)))  # 效果同等与 tf.norm(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<tf.Tensor: shape=(), dtype=float32, numpy=96.99484>"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "a = tf.ones([4,28,28,3])\n",
    "tf.norm(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<tf.Tensor: shape=(2,), dtype=float32, numpy=array([1.4142135, 1.4142135], dtype=float32)>"
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "source": [
    "b = tf.ones([2,2])\n",
    "tf.norm(b,ord=2,axis=1)  # 横(1)轴平方和开根号,ord表示第几范数(2范数平方和根号,1范数平方和)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<tf.Tensor: shape=(), dtype=float32, numpy=4.0>"
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "source": [
    "tf.norm(b,ord=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<tf.Tensor: shape=(2,), dtype=float32, numpy=array([2., 2.], dtype=float32)>"
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "source": [
    "tf.norm(b,ord=1,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(<tf.Tensor: shape=(), dtype=float32, numpy=-1.5540125>,\n <tf.Tensor: shape=(), dtype=float32, numpy=2.2461195>,\n <tf.Tensor: shape=(), dtype=float32, numpy=0.05438391>)"
     },
     "metadata": {},
     "execution_count": 20
    }
   ],
   "source": [
    "a = tf.random.normal([4,10])\n",
    "tf.reduce_min(a),tf.reduce_max(a),tf.reduce_mean(a)  # 最小值、最大值、均值(全局)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(<tf.Tensor: shape=(4,), dtype=float32, numpy=array([-1.2713498, -1.5540125, -1.5400875, -1.0738947], dtype=float32)>,\n <tf.Tensor: shape=(4,), dtype=float32, numpy=array([2.2461195, 1.4279553, 1.4034164, 1.5807836], dtype=float32)>,\n <tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 0.06974559,  0.08150292, -0.1043442 ,  0.17063133], dtype=float32)>)"
     },
     "metadata": {},
     "execution_count": 22
    }
   ],
   "source": [
    "tf.reduce_min(a,axis=1),tf.reduce_max(a,axis=1),tf.reduce_mean(a,axis=1)  # 在1轴返回 最小值、最大值、均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "TensorShape([10])"
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "source": [
    "tf.argmax(a).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<tf.Tensor: shape=(10,), dtype=int64, numpy=array([0, 2, 2, 2, 2, 2, 1, 1, 1, 3], dtype=int64)>"
     },
     "metadata": {},
     "execution_count": 26
    }
   ],
   "source": [
    "tf.argmax(a),  # 默认0轴，返回最大值的索引位置\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<tf.Tensor: shape=(10,), dtype=int64, numpy=array([0, 2, 2, 2, 2, 2, 1, 1, 1, 3], dtype=int64)>"
     },
     "metadata": {},
     "execution_count": 27
    }
   ],
   "source": [
    "tf.argmin(a)  # 返回最小值索引位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = tf.constant([1,2,3,4,5])\n",
    "b = tf.range(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<tf.Tensor: shape=(5,), dtype=bool, numpy=array([False, False, False, False, False])>"
     },
     "metadata": {},
     "execution_count": 31
    }
   ],
   "source": [
    "tf.equal(a,b)  # 逐元素比较a,b.返回True、False的集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<tf.Tensor: shape=(), dtype=int32, numpy=0>"
     },
     "metadata": {},
     "execution_count": 32
    }
   ],
   "source": [
    "res = tf.equal(a,b)\n",
    "tf.reduce_sum(tf.cast(res,dtype=tf.int32))  # 将结果列表转为0-1类型,并求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "Unique(y=<tf.Tensor: shape=(3,), dtype=int32, numpy=array([4, 2, 3])>, idx=<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 1, 0, 2])>)"
     },
     "metadata": {},
     "execution_count": 33
    }
   ],
   "source": [
    "a = tf.constant([4,2,2,4,3])\n",
    "tf.unique(a)  # 去除重复元素"
   ]
  }
 ]
}